<?php
namespace app\bis\controller;
use app\bis\controller\Base;
class Deal extends Base{
       public function index(){
           $bisId=$this->getLoginUser()->bis_id;
           $deals=model("Deal")->getNormalDeal($bisId);
          
           return $this->fetch("",[
               "deals"=>$deals,
           ]);
       }
    public function add(){
        $bisId=$this->getLoginUser()->bis_id;
       //获取城市的一级栏目
        $citys=model("City")->getNormalFirstCitys();
        //获取服务类别栏目
        $categorys=model("Category")->getNormalFirstCategorys();
        $bislocations=model("BisLocation")->getNormalLocaltionsByBisId($bisId);
        return  $this->fetch('',[
            'citys'=>$citys,
            'categorys'=>$categorys,
            'bislocations'=>$bislocations,
        ]);
    }
    
    public function save(){
       $bisId = $this->getLoginUser()->bis_id;
       if(!request()->isPost()){
           $this->error("非法请求");
        }  
       $data=input("post.");
       $validate= validate("Deal");   
       if(!$validate->check($data)){
         $this->error($validate->getError());
        }  
          
         //category_path
          $data['cat']="";
         if(!empty($data['se_category_id'])){
             $data['cat']= implode(",",$data['se_category_id']);
          }
          
         // print_r($data['cat']);exit;
        //location
            $location=model("BisLocation")->get($data["location_ids"][0]);
            $deals = [
                'bis_id' => $bisId,
                'name' => $data['name'],
                'image' => $data['image'],
                'category_id' => $data['category_id'],
                'se_category_id' =>empty($data['se_category_id']) ? '' : implode(',', $data['se_category_id']),
                'category_path'=>$data['cat'],
                'city_id' => $data['city_id'],
                'location_ids' => empty($data['location_ids']) ? '' : implode(',', $data['location_ids']),
                'start_time' => strtotime($data['start_time']),
                'end_time' => strtotime($data['end_time']),
                'total_count' => $data['total_count'],
                'origin_price' => $data['origin_price'],
                'current_price' => $data['current_price'],
                'coupons_begin_time' => strtotime($data['coupons_begin_time']),
                'coupons_end_time' => strtotime($data['coupons_end_time']),
                'notes' => $data['notes'],
                'description' => $data['description'],
                'bis_account_id' => $this->getLoginUser()->id,
                'xpoint' => $location->xpoint,
                'ypoint' => $location->ypoint,
                'status'=>0,//申请
            ];
           $id=model("deal")->add($deals);
           if($id){
               $this->success("添加成功",url("deal/index"));
           }else{
               $this->error("添加失败");
           }
           
    }
    
    
    
}

